package cn.bluemobi.platform.controller.commodity;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.bluemobi.common.core.dto.MapDto;
import cn.bluemobi.common.core.dto.PageCondition;
import cn.bluemobi.platform.core.controller.PlatformBaseController;
import cn.bluemobi.platform.core.dto.DatatablePage;
import cn.bluemobi.platform.entity.commodity.Commodity;
import cn.bluemobi.platform.service.CommodityService;

/**
 * 
 * @author lvxh
 * 
 */
@Controller
@RequestMapping("/admin/commodity")
public class CommodityController extends PlatformBaseController {

	@Autowired
	private CommodityService commodityService;

	@RequestMapping("/findMerchantList")
	@ResponseBody
	public List<Map<String, Object>> findMerchantList() {
		return commodityService.findMerchantList();
	}

	@RequestMapping("/findCategoryList")
	@ResponseBody
	public List<Map<String, Object>> findCategoryList(
			@RequestParam("level") String level,
			@RequestParam(value = "parent", required = false) String parent) {
		return commodityService.findCategoryList(level, parent);
	}

	@RequestMapping("/toCommodityPage")
	public String toCommodityPage(Model model) {
		return "commodity/commodityList";
	}

	@RequestMapping("/findCommoditys")
	@ResponseBody
	public DatatablePage findCommoditys(PageCondition cond) {
		return new DatatablePage(cond, commodityService.findCommoditys(cond,
				getUser()));
	}

	@RequestMapping("/toCommodityDetailPage")
	public String toCommodityDetailPage(
			@RequestParam(value = "commodityId", required = false) String commodityId,
			ModelMap model) {
		if (!StringUtils.isEmpty(commodityId)) {
			Commodity commodity = commodityService
					.findCommodityDetail(commodityId);
			model.put("commodity", commodity);
		}
		return "commodity/commodityDetail";
	}

	@RequestMapping("/saveCommodity")
	@ResponseBody
	public MapDto saveCommodity(Commodity commodity) {
		return commodityService.saveOrUpdate(commodity, getUser());
	}

	@RequestMapping("/deleteCommodity")
	@ResponseBody
	public MapDto deleteById(@RequestParam("commodityId") String commodityId) {
		return commodityService.deleteCommodity(commodityId, getUser());
	}

}
